package com.kuang.snake.service;

import com.sun.el.lang.ELArithmetic;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

    int lenth;
    int[] snakex = new int[600]; //蛇的坐标x
    int[] snakey = new int[600]; //蛇的坐标y

    String fx ; //L:左 R:右 U:上 D:下

    boolean isStart = false; //游戏是否开始

    Timer t = new Timer(100,this);//定时器


    //定义一个事物
    int foodx;
    int foody;
    Random random = new Random();

    public GamePanel(){
        init();

        //获取键盘监听
        this.setFocusable(true);
        this.addKeyListener(this);
        t.start();
    }


    public void init(){
        lenth = 3;
        snakex[0] = 100;snakey[0] = 100; //头部坐标
        snakex[1] = 75;snakey[1] = 100; //第一个头部坐标
        snakex[2] = 50;snakey[2] = 100; //第二个头部坐标

        fx = "R";

        foodx = 25 + 25 * random.nextInt(34);
        foody = 75 + 25 * random.nextInt(24);

    }


    //画板  画界面  画蛇
    /**
     * Graphics : 画笔
     * */
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g); //清屏
        this.setBackground(Color.white);//设置背景颜色
/*
        //绘制广告栏
        Data.header.paintIcon(this,g,25,11);*/

        //绘制游戏区域
        g.fillRect(25,75,850,600);

        if (fx.equals("R")){
            Data.right.paintIcon(this,g,snakex[0],snakey[0]);
        }else if (fx.equals("L")){
            Data.left.paintIcon(this,g,snakex[0],snakey[0]);
        }else if (fx.equals("U")){
            Data.up.paintIcon(this,g,snakex[0],snakey[0]);
        } else if (fx.equals("D")){
            Data.down.paintIcon(this,g,snakex[0],snakey[0]);
        }

        /*//静态小蛇
        Data.right.paintIcon(this,g,snakex[0],snakey[0]);
        Data.body.paintIcon(this,g,snakex[1],snakey[1]);
        Data.body.paintIcon(this,g,snakex[2],snakey[2]);*/

        for (int i = 1; i < lenth; i++){
            Data.body.paintIcon(this,g,snakex[i],snakey[i]);
        }


        //画食物
        Data.food.paintIcon(this,g,foodx,foody);


        //提示游戏开始
        if (isStart == false){
            g.setColor(Color.white); //设置颜色
            g.setFont(new Font("微软雅黑",Font.BOLD,40)); //设置字体
            g.drawString("按下空格键开始",300,300);
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //按下未释放
        int keyCode = e.getKeyCode();

        if (keyCode == KeyEvent.VK_SPACE){
            //如果按下空格键
            isStart = !isStart;
            repaint();//刷新界面
        }

        if (keyCode == KeyEvent.VK_LEFT){
            fx = "L";
        }else if (keyCode == KeyEvent.VK_RIGHT){
            fx = "R";
        }else if (keyCode == KeyEvent.VK_UP){
            fx = "U";
        }else if (keyCode == KeyEvent.VK_DOWN){
            fx = "D";
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        //执行定时操作
        if (isStart){
            //右移
            for (int i = lenth - 1; i > 0; i--){
                snakex[i] = snakex[i-1];
                snakey[i] = snakey[i-1];
            }
            //头部移动 通过控制方向
            if (fx.equals("R")){
                snakex[0] = snakex[0] + 25;
                //边界判断
                if (snakex[0] > 850){snakex[0] = 25;}
            }else if (fx.equals("L")) {
                snakex[0] = snakex[0] - 25;
                //边界判断
                if (snakex[0] < 25){snakex[0] = 850;}
            }else if (fx.equals("U")){
                snakey[0] = snakey[0] - 25;
                //边界判断
                if (snakey[0] < 75){snakey[0] = 650;}
            }else if (fx.equals("D")){
                snakey[0] = snakey[0] + 25;
                //边界判断
                if (snakey[0] > 650){snakey[0] = 75;}
            }

            //如果小蛇的头和食物的坐标重合
            if (snakey[0] == foody && snakex[0] == foodx){
                //长度+1
                lenth += 1;

                //重新生成食物
                foodx = 25 + 25 * random.nextInt(34);
                foody = 75 + 25 * random.nextInt(24);
            }

            repaint();
        }
        t.start();
    }
}
